﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ReportLibrary
{
    public class ReportUtil
    {
        public static string VoucherDate(object p_VoucherDate)
        {
            try
            {
                DateTime voucherDate = Convert.ToDateTime(p_VoucherDate);
                return "Ngày " + voucherDate.Day + " tháng " + voucherDate.Month + " năm " + voucherDate.Year;
            }
            catch 
            {
                return "";
            }
        }
        public static string VoucherDateFormat(object p_VoucherDate)
        {
            if (p_VoucherDate == null)
                return "";
            try
            {
                DateTime voucherDate = Convert.ToDateTime(p_VoucherDate);
                return voucherDate.ToString("dd/MM/yyyy")!="01/01/1900"?voucherDate.ToString("dd/MM/yyyy"):"";
            }
            catch
            {
                return "";
            }
        }
        public static string ReceiptVoucherId2String(object p_VoucherId) {
            string voucherString = p_VoucherId.ToString();           
            while (voucherString.Length < 5)
                voucherString = "0" + voucherString;

            voucherString = "PT" + voucherString;

            return voucherString;
        }
        public static string Num2Text(object p_Number) {
            return ConvertNum2Text(p_Number.ToString()) + " đồng";
        }
        public static string ToDay2String()
        {
            DateTime voucherDate = DateTime.Now;            
            return "Ngày " + voucherDate.Day + " tháng " + voucherDate.Month + " năm " + voucherDate.Year;           
        }
        public static string Num2VNDCurrency(object p_Amount) {
            try
            {
                double amount = Convert.ToDouble(p_Amount);
                return amount.ToString("###,##0.00") + " VNĐ";
            }
            catch
            {
                return p_Amount.ToString() + " VNĐ";
            }
        }
        public static string Convert2NumericFormat(object p_String)
        {
            if (p_String == null)
                return ""; 
            string p = p_String.ToString();
            string prefixNumber;
            string integerPart;
            string decimalPart;
            string unSignedString;
            prefixNumber = p[0] == '-' ? "-" : "";
            unSignedString = p[0] == '-' ? p.Substring(1) : p;
            if (unSignedString.Contains('.'))
            {
                integerPart = unSignedString.Split('.')[0];
                decimalPart = "." + p.Split('.')[1];
            }
            else
            {
                integerPart = unSignedString;
                decimalPart = "";
            }

            int index = 0;
            string result = "";
            for (int i = integerPart.Length - 1; i >= 0; i--)
            {
                if (index == 3)
                {
                    result = integerPart[i] + "." + result;
                    index = 1;
                }
                else
                {
                    result = integerPart[i] + result;
                    index++;
                }
            }

           return  prefixNumber + result + decimalPart;     
        }
        public static string Convert2FrenchNumericFormat(object p_String)
        {
            if (p_String == null)
                return "";
            string p = p_String.ToString();
            string prefixNumber;
            string integerPart;
            string decimalPart;
            string unSignedString;
            prefixNumber = p[0] == '-' ? "-" : "";
            unSignedString = p[0] == '-' ? p.Substring(1) : p;
            if (unSignedString.Contains('.'))
            {
                integerPart = unSignedString.Split('.')[0];
                decimalPart = "." + p.Split('.')[1];
            }
            else
            {
                integerPart = unSignedString;
                decimalPart = "";
            }

            int index = 0;
            string result = "";
            for (int i = integerPart.Length - 1; i >= 0; i--)
            {
                if (index == 3)
                {
                    result = integerPart[i] + "," + result;
                    index = 1;
                }
                else
                {
                    result = integerPart[i] + result;
                    index++;
                }
            }

            return prefixNumber + result + decimalPart;
        }
        public static string Convert2MinusNumericFormat(object p_String)
        {
            if (p_String == null)
                return "";
            string p = p_String.ToString();
            string prefixNumber;
            string integerPart;
            string decimalPart;
            string unSignedString;
            prefixNumber = p[0] == '-' ? "-" : "";
            unSignedString = p[0] == '-' ? p.Substring(1) : p;
            if (unSignedString.Contains('.'))
            {
                integerPart = unSignedString.Split('.')[0];
                decimalPart = "." + p.Split('.')[1];
            }
            else
            {
                integerPart = unSignedString;
                decimalPart = "";
            }

            int index = 0;
            string result = "";
            for (int i = integerPart.Length - 1; i >= 0; i--)
            {
                if (index == 3)
                {
                    result = integerPart[i] + "," + result;
                    index = 1;
                }
                else
                {
                    result = integerPart[i] + result;
                    index++;
                }
            }

            if (prefixNumber.Equals("-"))
                return "(" + result + decimalPart + ")";
            return prefixNumber + result + decimalPart;
        }
        public static string CheckReceiptVoucher(object p_VoucherIdString) {
            if (p_VoucherIdString == null)
                return "";
            if (p_VoucherIdString.ToString().Contains("PT"))
                return p_VoucherIdString.ToString();
            else
                return "";
        }
        public static string CheckPaymentVoucher(object p_VoucherIdString)
        {
            if (p_VoucherIdString == null)
                return "";
            if (p_VoucherIdString.ToString().Contains("PC"))
                return p_VoucherIdString.ToString();
            else
                return "";
        }
        public static string CheckStoreInputVoucher(object p_VoucherIdString)
        {
            if (p_VoucherIdString == null)
                return "";
            if (p_VoucherIdString.ToString().Contains("PNK"))
                return p_VoucherIdString.ToString();
            else
                return "";
        }
        public static string CheckStoreOutputVoucher(object p_VoucherIdString)
        {
            if (p_VoucherIdString == null)
                return "";
            if (p_VoucherIdString.ToString().Contains("PXK"))
                return p_VoucherIdString.ToString();
            else
                return "";
        }
        #region convert method
        private static string ConvertNum2Text(string Sonhap)
        {
            string result = "";
            switch (Sonhap.Length)
            {
                case 0: result = "";
                    break;
                case 1: result = Sodonvi(Sonhap);
                    break;
                case 2:
                    {

                        result = Sohangchuc(Sonhap);

                    }
                    break;
                case 3:
                    {
                        result = Sohangtram(Sonhap);
                    }
                    break;
                case 4:
                    {
                        result = Sohangngan(Sonhap);

                    }
                    break;
                case 5:
                    {
                        if (Sonhap.Substring(2, 3).Equals("000"))
                        {
                            result = Sohangchuc(Sonhap.Substring(0, 2)) + " ngàn ";
                        }
                        else
                            result = Sohangchuc(Sonhap.Substring(0, 2)) + " ngàn " + Sohangtram(Sonhap.Substring(2, 3));
                    }
                    break;
                case 6:
                    {
                        if (Sonhap.Substring(3, 3).Equals("000"))
                        {
                            result = Sohangtram(Sonhap.Substring(0, 3)) + " ngàn ";
                        }
                        else
                            result = Sohangtram(Sonhap.Substring(0, 3)) + " ngàn " + Sohangtram(Sonhap.Substring(3, 3));

                    }
                    break;
                case 7:
                    {
                        //if (Sonhap.Substring(3, 4).Equals("0000"))
                        //{
                        // result = Sodonvi(Sonhap.Substring(0, 1)) + " triệu ";
                        //}
                        //else
                        result = Sodonvi(Sonhap.Substring(0, 1)) + " triệu " + Sohangtram(Sonhap.Substring(1, 3)) + " ngàn " + Sohangtram(Sonhap.Substring(4, 3));
                    }
                    break;
                case 8:
                    {
                        if (Sonhap.Substring(2, 6).Equals("000000"))
                        {
                            result = Sohangchuc(Sonhap.Substring(0, 2)) + " triệu ";
                        }
                        else
                            result = Sohangchuc(Sonhap.Substring(0, 2)) + " triệu " + Sohangtram(Sonhap.Substring(2, 3)) +
                            " ngàn " + Sohangtram(Sonhap.Substring(5, 3));
                    }
                    break;
                case 9:
                    {
                        if (Sonhap.Substring(3, 6).Equals("000000"))
                        {
                            result = Sohangtram(Sonhap.Substring(0, 3)) + " triệu ";
                        }
                        else
                            result = Sohangtram(Sonhap.Substring(0, 3)) + " triệu " + Sohangtram(Sonhap.Substring(3, 3)) +
                            " ngàn " + Sohangtram(Sonhap.Substring(6, 3));
                    }
                    break;
                case 10:
                    {
                        if (Sonhap.Substring(1, 9).Equals("000000000"))
                        {
                            result = Sodonvi(Sonhap.Substring(0, 1)) + " tỷ ";
                        }
                        else
                            result = Sodonvi(Sonhap.Substring(0, 1)) + " tỷ " + Sohangtram(Sonhap.Substring(1, 3)) + " triệu " + Sohangtram(Sonhap.Substring(4, 3)) +
                            " ngàn " + Sohangtram(Sonhap.Substring(7, 3));
                    }
                    break;
                case 11:
                    {
                        if (Sonhap.Substring(2, 9).Equals("000000000"))
                        {
                            result = Sohangchuc(Sonhap.Substring(0, 2)) + " tỷ ";
                        }
                        else
                            result = Sohangchuc(Sonhap.Substring(0, 2)) + " tỷ " + Sohangtram(Sonhap.Substring(2, 3)) + " triệu " + Sohangtram(Sonhap.Substring(5, 3)) +
                            " ngàn " + Sohangtram(Sonhap.Substring(6, 3));
                    }
                    break;
                case 12:
                    {
                        if (Sonhap.Substring(3, 9).Equals("000000000"))
                        {
                            result = Sohangtram(Sonhap.Substring(0, 3)) + " tỷ ";
                        }
                        else
                            result = Sohangtram(Sonhap.Substring(0, 3)) + " tỷ " + Sohangtram(Sonhap.Substring(3, 3)) + " triệu " + Sohangtram(Sonhap.Substring(6, 3)) +
                            " ngàn " + Sohangtram(Sonhap.Substring(9, 3));
                    }
                    break;

            }
            return result.Replace(" ", " ").Replace("mươi một", "mươi mốt").Replace("mươi năm", "mươi lăm");
        }
        private static string ChuanHoa(string st)
        {
            string st1 = "";
            return st1.Trim();
        }
        private static string Sohangngan(string So)
        {
            string result = "";
            if (So.Equals("1000"))
                result = " một ngàn ";
            else
            {
                result = Sodonvi(So.Substring(0, 1)) + " ngàn ";
                if (So.Substring(1, 1).Equals("0"))
                {

                    if (So.Substring(2, 1).Equals("0"))
                    {
                        if (!So.Substring(3, 1).Equals("0"))
                        {
                            result += Sodonvi(So.Substring(1, 1)) + " trăm ";
                            result += " lẻ " + Sodonvi(So.Substring(So.Length - 1));
                        }
                    }
                    else
                    {
                        result += Sodonvi(So.Substring(1, 1)) + " trăm ";
                        result += Sohangchuc(So.Substring(2, 2));
                    }
                }
                else
                    result += Sohangtram(So.Substring(1, 3));
            }
            return result;


        }
        private static string Sohangtram(string So)
        {
            string result = "";
            if (So.Equals("100"))
                result = " một trăm ";
            else
            {
                result += Sodonvi(So.Substring(0, 1)) + " trăm ";
                if (So.Substring(1, 1).Equals("0"))
                {
                    if (!So.Substring(2, 1).Equals("0"))
                        result += " lẻ " + Sodonvi(So.Substring(2, 1));
                }
                else
                    result += Sohangchuc(So.Substring(1, 2));
            }
            return result;

        }
        private static string Sohangchuc(string So)
        {
            string result = "";
            if (So.Equals("10"))
                result = " mười ";
            else
            {
                if (So.Substring(0, 1).Equals("1"))
                    result += " mười " + Sodonvi(So.Substring(1, 1));
                else
                {
                    result += Sodonvi(So.Substring(0, 1)) + " mươi ";
                    if (!So.Substring(1, 1).Equals("0"))
                        result += Sodonvi(So.Substring(1, 1));


                }
            }
            return result;
        }
        private static string Sodonvi(string So)
        {
            string result = "";
            switch (So)
            {
                case "0": result += " không ";
                    break;
                case "1": result += " một ";
                    break;
                case "2": result += " hai ";
                    break;
                case "3": result += " ba ";
                    break;
                case "4": result += " bốn ";
                    break;
                case "5": result += " năm ";
                    break;
                case "6": result += " sáu ";
                    break;
                case "7": result += " bảy ";
                    break;
                case "8": result += " tám ";
                    break;
                case "9": result += " chín ";
                    break;

            }
            return result;
        }
        #endregion
    }
}
